package com.senyin.servlet.staffInfo;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.senyin.common.BaseResponse;
import com.senyin.common.ErrorCode;
import com.senyin.common.ResultUtils;
import com.senyin.entity.StaffInfo;
import com.senyin.service.StaffInfoService;
import com.senyin.service.impl.StaffInfoServiceImpl;
import com.senyin.utils.ComMethods;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;

public class StaffModServlet extends HttpServlet {
    private StaffInfoService staffInfoService = new StaffInfoServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        String siUuid = req.getParameter("siUuid");
        String siId = req.getParameter("siId");
        String siName = req.getParameter("siName");
        Integer siSex = Integer.valueOf(req.getParameter("siSex"));
        String siBirth = req.getParameter("siBirth");
        String psId = req.getParameter("psId");
        String dpId = req.getParameter("dpId");
        Integer siAuth = Integer.valueOf(req.getParameter("siAuth"));
        StaffInfo staffInfo = null;
        BaseResponse baseResponse = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            staffInfo = new StaffInfo(siUuid, siId, siName, siSex, ComMethods.getDate(siBirth), psId, dpId, siAuth);
            baseResponse = staffInfoService.modStaff(staffInfo, req);
        } catch (ParseException | SQLException e) {
            resp.getWriter().write("error");
        }
        resp.getWriter().write(objectMapper.writeValueAsString(baseResponse));

    }
}
